﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditGifts : AdminPageBase
    {
        private GiftsInfo _giftsInfo = new GiftsInfo();
        private readonly GiftsController _giftsController = new GiftsController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["GiftsId"]))
                _giftsInfo = _giftsController.GetGifts(int.Parse(Request.QueryString["GiftsId"]));

            if (Page.IsPostBack) return;

            LoadUser();
            LoadGiftsType();
          
            if (_giftsInfo.GiftsId == -1) return;

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListGifts.aspx?xml=Gifts");
        }

        private void SaveData()
        {
            _giftsInfo.GiftsTypeId = int.Parse(ddlGiftsType.SelectedValue);
            _giftsInfo.GiftsCode = txtGiftsCode.Text;
            _giftsInfo.GiftsName = txtGifts.Text;
            _giftsInfo.GiftsPriceAdult = decimal.Parse(txtPriceAdult.Text);
            _giftsInfo.GiftsPriceChild = decimal.Parse(txtPriceChild.Text);
            _giftsInfo.GiftsPriceBaby = decimal.Parse(txtPriceBaby.Text);
            _giftsInfo.QuantityAdult= int.Parse(txtQuantityAdult.Text);
            _giftsInfo.QuantityChild = int.Parse(txtQuantityChild.Text);
            _giftsInfo.QuantityBaby = int.Parse(txtQuantityBaby.Text);
            if (_giftsInfo.GiftsId == -1)
            {
                _giftsInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _giftsInfo.CreatedDate = DateTime.Now;
                _giftsController.InsertGifts(_giftsInfo);
            }
            else
            {
                _giftsInfo.UpdatedBy = int.Parse(ddlCreatedBy.SelectedValue);
                _giftsInfo.UpdatedDate = DateTime.Now;
                _giftsController.UpdateGifts(_giftsInfo);
            }

            Response.Redirect("/Pages/ListGifts.aspx?xml=Gifts");
        }

        private void RenderForm()
        {
            ddlGiftsType.SelectedValue = _giftsInfo.GiftsTypeId.ToString();
            txtGifts.Text = _giftsInfo.GiftsName;
            ddlCreatedBy.SelectedValue = _giftsInfo.CreatedBy.ToString();
            txtGiftsCode.Text = _giftsInfo.GiftsCode.ToString();
            txtPriceAdult.Text = _giftsInfo.GiftsPriceAdult.ToString();
            txtPriceChild.Text = _giftsInfo.GiftsPriceChild.ToString();
            txtPriceBaby.Text = _giftsInfo.GiftsPriceBaby.ToString();
            txtQuantityAdult.Text = _giftsInfo.QuantityAdult.ToString();
            txtQuantityChild.Text = _giftsInfo.QuantityChild.ToString();
            txtQuantityBaby.Text = _giftsInfo.QuantityBaby.ToString();
       }
 
       
      private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadGiftsType()
        {
            ddlGiftsType.DataSource = new GiftsTypeController().ListGiftsType().OrderBy(v=>v.GiftsTypeName);
            ddlGiftsType.DataTextField = "GiftsTypeName";
            ddlGiftsType.DataValueField = "GiftsTypeId";
            ddlGiftsType.DataBind();

            ddlGiftsType.Items.Insert(0, new ListItem("--- Chọn loại quà ---", "-1"));
        }
    }
}